package com.demo.boss.controller;

import com.demo.boss.models.Result;
import com.demo.boss.models.dto.OperatorLoginDto;
import com.demo.boss.models.menus.SubMenu;
import com.demo.boss.models.request.OperatorLoginRequest;
import com.demo.boss.server.OperatorService;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin // 解决跨域操作
public class OperatorController {
    @Resource
    private OperatorService operatorService;
    /**
     * 操作员登录接口
     */
    @PostMapping("/operator/login")
    public Result login(@RequestBody @Validated OperatorLoginRequest loginRequest){
        // 1、参数验证 if 语句 和 Bean Validation
        // 2、业务逻辑，到数据库查询用户，比较密码
        OperatorLoginDto dto = operatorService.login(loginRequest);

        return Result.ok(dto);
    }
    /**
     * 在 request header 中添加，登录用户的标识 operatorId
     * 首页菜单以及子菜单
     */
    @GetMapping("/operator/menus")
    public Result getMenus(@RequestHeader("operatorId") Long operatorId){
        List<SubMenu> subMenuList = operatorService.queryMenusByOperatorId(operatorId);
        return Result.ok(subMenuList);
    }
}
